package com.sk.places.places2.presentation.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.omg.CORBA.NameValuePair;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

import com.sk.places.places2.business.service.PlaceService;
import com.sk.places.places2.business.service.PlaceServiceImpl;
import com.sk.places.places2.service.entity.Place;
import com.sk.places.places2.service.entity.PlaceType;

public class InitDataLoaderAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	protected Logger logger = Logger.getLogger("InitDataLoaderAction") ;
	private List<PlaceType> placeTypes;
	private Long[] selectedPlaceTypes ; 
	//private PlaceService placeService;
	ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml");
	private PlaceService placeService = (PlaceService)context.getBean("placeService") ; 
	private List<Place> placeList = new ArrayList<Place>();
	private String actionFlag;
	private String visitingMonth;
	private Date visitDate;
	private boolean isFetched;
	
	
	public String execute() {
		
		try {
			if(placeTypes == null) {
				System.out.println("place types null");
				placeTypes = placeService.getPlacesTypes() ;
			}
			if(this.actionFlag!=null && this.actionFlag.equals("fetchplaces")){
				
				this.placeList = placeService.getPlacesByTypeAndVistingMonth(Arrays.asList(selectedPlaceTypes), new SimpleDateFormat("MM").format(visitDate));
				System.out.println("placeList : " + placeList.size());
				if(placeList.size()>0){
					isFetched = true;
				}
				else{
					isFetched = false;
				}
			}
			return SUCCESS ; 
		}
		catch(Exception e) {
			System.out.println("Exception");
			e.printStackTrace();
			System.out.println(ExceptionUtils.getFullStackTrace(e));
			logger.severe(ExceptionUtils.getFullStackTrace(e)) ;
			return ERROR ; 
		}
		finally{
			this.actionFlag = null;
		}
	}


	public List<PlaceType> getPlaceTypes() {
		return placeTypes;
	}


	public void setPlaceTypes(List<PlaceType> placeTypes) {
		this.placeTypes = placeTypes;
	}


	public Long[] getSelectedPlaceTypes() {
		return selectedPlaceTypes;
	}


	public void setSelectedPlaceTypes(Long[] selectedPlaceTypes) {
		this.selectedPlaceTypes = selectedPlaceTypes;
	}


	public PlaceService getPlaceService() {
		return placeService;
	}


	public void setPlaceService(PlaceService placeService) {
		this.placeService = placeService;
	}
	
	public String getActionFlag() {
		return actionFlag;
	}


	public void setActionFlag(String actionFlag) {
		this.actionFlag = actionFlag;
	}
	
	public String getVisitingMonth(){
		return this.visitingMonth;
	}
	
	public void setVisitingMonth(String visitingMonth){
		this.visitingMonth = visitingMonth;
	}


	public List<Place> getPlaceList() {
		return placeList;
	}


	public void setPlaceList(List<Place> placeList) {
		this.placeList = placeList;
	}
	

	public boolean getIsFetched() {
		return isFetched;
	}


	public void setIsFetched(boolean isFetched) {
		this.isFetched = isFetched;
	}


	public Date getVisitDate() {
		return visitDate;
	}


	public void setVisitDate(Date visitDate) {
		this.visitDate = visitDate;
	}


}
